package summary;

/**
 * @Author: 海琳琦
 * @Date: 2022/4/13 14:30
 * https://leetcode-cn.com/problems/minimum-path-sum/
 */
public class Title64 {

    /**
     * dp[i][j]表示到达i,j时最小路径和
     * @param grid
     * @return
     */
    public int minPathSum(int[][] grid) {
        int n = grid.length;
        int m = grid[0].length;
        int[][] dp = new int[n][m];
        //初始化
        int sum = 0;
        for (int i = 0; i < n; i++) {
            sum += grid[i][0];
            dp[i][0] = sum;
        }
        sum = 0;
        for (int j = 0; j < m; j++) {
            sum += grid[0][j];
            dp[0][j] = sum;
        }
        for (int i = 1; i < n; i++) {
            for (int j = 1; j < m; j++) {
                dp[i][j] = Math.min(dp[i - 1][j], dp[i][j - 1]) + grid[i][j];
            }
        }
        return dp[n - 1][m - 1];
    }

    public static void main(String[] args) {

    }
}
